package testtiles;

import testtiles.object.Messages;
import butines.game.AbstractState;
import butines.game.Message;


public class UnitStayState extends UnitState {

	private static final UnitStayState instance = new UnitStayState();

	public static UnitStayState getInstance() {
		return instance;
	}

	private boolean ifUnitMoveBraking(Unit unit) {
		if (unit.getSpeed() > 0.1) {
			unit.getSteering().braking(0.7f);
			unit.getAnimationSet().getWalk().play();
			return true;
		}
		return false;
	}
	
	private boolean keepStay(Unit unit) {
		unit.getAnimationSet().getStay().play();
		unit.setSpeed(0);
		return true;
	}
	
	private void onCollideWith(Unit unit, Unit other) {
		unit.getSteering().flee(other.getPosition());
	}
	
	@Override
	public void enter(Unit unit) {
		unit.setSpeed(0);
		unit.getAnimationSet().getStay().play();
	}
	
	@Override
	public void leave(Unit unit) {
		
	}

	@Override
	public void execute(Unit unit) {
		
		if (ifUnitMoveBraking(unit)) {
			return;
		}
		
		if (keepStay(unit)) {
			
		}
	}
	
	@Override
	public boolean onMessage(Message message) {
		Messages type = message.getType();
		switch (type) {
			case COLLIDED_WITH: {
				Unit other = message.getSender();
				Unit self = message.getReceiver();
				onCollideWith(self, other);
				return true;
			}
		}
		return false;
	}

}
